<?php
require_once '../../common.inc.php';
require_once("../API/qqConnectAPI.php");
/* 
**  php分别模拟发送GET与POST请求 
** 
*/ 
 
function httpRequest($url,$method,$params=array()){ 
    if(trim($url)==''||!in_array($method,array('get','post'))||!is_array($params)){ 
        return false; 
    } 
    $curl=curl_init(); 
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($curl,CURLOPT_HEADER,0 ) ; 
    switch($method){ 
        case 'get': 
            $str='?'; 
            foreach($params as $k=>$v){ 
                $str.=$k.'='.$v.'&'; 
            } 
            $str=substr($str,0,-1); 
            $url.=$str;//$url=$url.$str; 
            curl_setopt($curl,CURLOPT_URL,$url); 
        break; 
        case 'post': 
            curl_setopt($curl,CURLOPT_URL,$url); 
            curl_setopt($curl,CURLOPT_POST,1 ); 
            curl_setopt($curl,CURLOPT_POSTFIELDS,$params); 
        break; 
        default: 
            $result=''; 
        break; 
    } 
    $result=curl_exec($curl); 
    curl_close($curl); 
    return $result; 
}

$qc = new QC();
$acs = $qc->qq_callback();
$oid = $qc->get_openid();
$qc = new QC($acs,$oid);
$ret = $qc->get_user_info();

/**
 * Each engineer has a duty to keep the code elegant
 * Author @liuhuiwu<13750088090@sina.cn>
 * 调用用户中心第三方登录接口 20161112
 */
$url = $ucurl . '/user/thirdLogin';
$params = array(
    'version' => '1.0.1',
    'user_id' => $oid,
    'username' => $ret['nickname'],
    'head_icon' => $ret['head_icon'],
    'plat_from' => 'QQ',
    'login_time' => time(),
    'device_id' => 'c92be85cb5c5ec5aea0d28f3f524d44274fa9aee1409285e23e7af015ed6b885',
    'device_from' => 'ios',
    'third_num' => '513730844',
);
$res = httpRequest($url, $method='get', $params);
$result = json_decode($res,true);

if($ret['ret'] == 0){
	$ret['openid'] = $oid;

	$query=$db->query("select uid from {$tablepre}members where password='{$oid}' limit 1");
	if(!$db->num_rows($query)) {
		$regtime=gdate();
		if(isset($_COOKIE['tg'])) {
			$tuser = userinfo($_COOKIE['tg'],'{username}');
		} else {
			$tuser = rand_kefu();
		}

		if($cfg['config']['regaudit']=='1')
			$state='0';
		else
			$state='1';

		$db->query("insert into {$tablepre}members(username,password,sex,email,regdate,regip,lastvisit,lastactivity,gold,realname,gid,phone,fuser,tuser,state)	values('{$ret['nickname']}','{$ret['openid']}','2','','$regtime','$onlineip','$regtime','$regtime','0','','1','','$tuser','$tuser','$state')");

		$uid=$db->insert_id();
		$db->query("replace into {$tablepre}memberfields (uid,nickname)	values('$uid','{$ret['nickname']}')	");
		
		
		$db->query("insert into  {$tablepre}msgs(rid,ugid,uid,uname,tuid,tname,mtime,ip,msg,type)
	values('{$cfg[config][id]}','1','{$uid}','{$ret['nickname']}','{$cfg[config][defvideo]}','{$cfg[config][defvideonick]}','".gdate()."','{$onlineip}','用户注册','2')
		");
		
		$cache = file_get_contents($ret['figureurl']);
		file_put_contents('../../../../face/p1/' . $uid . '.gif', $cache);
	}

	//$u = substr($ret['nickname'], 0, 6);
	$u = $ret['nickname'];
	qc_login($u, $oid, 2);
	// qq登录成功 数据插入用户中心绑定第三方登录Author @liuhuiwu<13750088090@sina.cn>

	setcookie("username", $u, gdate()+315360000, '/');
	header("location:" . $_SERVER['HOST_NAME'] . "/room");
} else {
	echo 'login error';
}


/**
Array
(
    [ret] => 0
    [msg] => 
    [is_lost] => 0
    [nickname] => 莉莉
    [gender] => 男
    [province] => 
    [city] => 布兰卡港
    [year] => 1986
    [figureurl] => http://qzapp.qlogo.cn/qzapp/101272484/11C70FA682572D107DA590F2D56489A6/30
    [figureurl_1] => http://qzapp.qlogo.cn/qzapp/101272484/11C70FA682572D107DA590F2D56489A6/50
    [figureurl_2] => http://qzapp.qlogo.cn/qzapp/101272484/11C70FA682572D107DA590F2D56489A6/100
    [figureurl_qq_1] => http://q.qlogo.cn/qqapp/101272484/11C70FA682572D107DA590F2D56489A6/40
    [figureurl_qq_2] => http://q.qlogo.cn/qqapp/101272484/11C70FA682572D107DA590F2D56489A6/100
    [is_yellow_vip] => 0
    [vip] => 0
    [yellow_vip_level] => 0
    [level] => 0
    [is_yellow_year_vip] => 0
    [openid] => 11C70FA682572D107DA590F2D56489A6
)
Array
(
    [ret] => 0
    [msg] => 
    [is_lost] => 0
    [nickname] => wolffy
    [gender] => 男
    [province] => 广东
    [city] => 珠海
    [year] => 1995
    [figureurl] => http://qzapp.qlogo.cn/qzapp/101299940/AE6E9E56132046BDE22069512DC5CFAE/30
    [figureurl_1] => http://qzapp.qlogo.cn/qzapp/101299940/AE6E9E56132046BDE22069512DC5CFAE/50
    [figureurl_2] => http://qzapp.qlogo.cn/qzapp/101299940/AE6E9E56132046BDE22069512DC5CFAE/100
    [figureurl_qq_1] => http://q.qlogo.cn/qqapp/101299940/AE6E9E56132046BDE22069512DC5CFAE/40
    [figureurl_qq_2] => http://q.qlogo.cn/qqapp/101299940/AE6E9E56132046BDE22069512DC5CFAE/100
    [is_yellow_vip] => 0
    [vip] => 0
    [yellow_vip_level] => 0
    [level] => 0
    [is_yellow_year_vip] => 0
    [openid] => AE6E9E56132046BDE22069512DC5CFAE

)*/
